<?php
/* * *********************************************
 * @类名:   page
 * @参数:   $cnt - 总记录数
 *          $pageSize - 一页显示的记录数
 *          $page - 当前页
 *          $url - 获取当前的url
 * @功能:   分页实现
 * @作者:   宋海阁
 */

class Page {

    private $cnt;          //总记录数
    private $pageSize;           //一页显示的记录数
    private $page;           //当前页
    private $maxPage;     //总页数
    private $startPage;              //起头页数
    private $endPage;             //结尾页数
    private $url;            //获取当前的url
    /*
     * $offsetPage
     * 页面显示的格式，显示链接的页数为2*$offsetPage+1。
     * 如$offsetPage=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页]
     */
    private $offsetPage;

    public function __construct($cnt = 1, $pageSize = 1, $page = 1, $url, $offsetPage = 2) {
        $this->cnt = $this->numeric($cnt);
        $this->pageSize = $this->numeric($pageSize);
        $this->page = $this->numeric($page);
        $this->maxPage = ceil($this->cnt / $this->pageSize);
        $this->url = $url;
        if ($this->cnt < 0)
            $this->cnt = 0;
        if ($this->page < 1)
            $this->page = 1;
        if ($this->maxPage < 1)
            $this->maxPage = 1;
        if ($this->page > $this->maxPage)
            $this->page = $this->maxPage;
        $this->limit = ($this->page - 1) * $this->pageSize;
        $this->startPage = $this->page - $offsetPage;
        $this->endPage = $this->page + $offsetPage;
        if ($this->startPage < 1) {
            $this->endPage = $this->endPage + (1 - $this->startPage);
            $this->startPage = 1;
        }
        if ($this->endPage > $this->maxPage) {
            $this->startPage = $this->startPage - ($this->endPage - $this->maxPage);
            $this->endPage = $this->maxPage;
        }
        if ($this->startPage < 1)
            $this->startPage = 1;
    }

    //检测是否为数字
    private function numeric($num) {
        if (strlen($num)) {
            if (!preg_match("/^[0-9]+$/", $num)) {
                $num = 1;
            } else {
                $num = substr($num, 0, 11);
            }
        } else {
            $num = 1;
        }
        return $num;
    }

    //地址替换
    private function page_replace($page) {
        return str_replace("{page}", $page, $this->url);
    }

    //首页
    private function home() {
        if ($this->page != 1) {
            return "<a href='" . $this->page_replace(1) . "' title='首页'>首页</a>";
        } else {
//            return "<p>首页</p>";
            return"<a href=\"javascript:;\" class=\"layui-laypage-prev layui-disabled\" data-page=\"0\">首页</a>";
        }
    }

    //上一页
    private function prev() {
        if ($this->page != 1) {
            return "<a href='" . $this->page_replace($this->page - 1) . "' title='上一页'>上一页</a>";
        } else {
//            return "<p>上一页</p>";
            return"<a href=\"javascript:;\" class=\"layui-laypage-prev layui-disabled\" data-page=\"0\">上一页</a>";
        }
    }

    //下一页
    private function next() {
        if ($this->page != $this->maxPage) {
            return "<a href='" . $this->page_replace($this->page + 1) . "' title='下一页'>下一页</a>";
        } else {
//            return"<p>下一页</p>";
            return"<a href=\"javascript:;\" class=\"layui-laypage-prev layui-disabled\" data-page=\"0\">下一页</a>";
        }
    }

    //尾页
    private function last() {
        if ($this->page != $this->maxPage) {
            return "<a href='" . $this->page_replace($this->maxPage) . "' title='尾页'>尾页</a>";
        } else {
//            return "<p>尾页</p>";
            return"<a href=\"javascript:;\" class=\"layui-laypage-prev layui-disabled\" data-page=\"0\">末页</a>";
        }
    }

    //输出
    public function getPageHtml($id = 'layui-laypage-1') {
        $str = "<div class='layui-box layui-laypage layui-laypage-default' id=" . $id . ">";
        $str.=$this->home();
        $str.=$this->prev();
        if ($this->startPage > 1) {
            $str.="<span>...</span>";
        }
        for ($i = $this->startPage; $i <= $this->endPage; $i++) {
            if ($i == $this->page) {
//                $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页' class='cur'>$i</a>";
                $str.="<span class='layui-laypage-curr'><em class='layui-laypage-em'></em><em>" . $i . "</em></span>";
            } else {
                $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页'>$i</a>";
            }
        }
        if ($this->endPage < $this->maxPage) {
            $str.="<span>...</span>";
        }
        $str.=$this->next();
        $str.=$this->last();
        $str.="<span>共<b>" . $this->maxPage .
            "</b>页<b>" . $this->cnt . "</b>条数据</span>";
        $str.="</div>";
        return $str;
    }

}

?>